Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement the relative phase Toffoli gates #3761

Merged
merged 21 commits into from
Feb 13, 2020

Conversation

Cryoris
Copy link
Contributor

@Cryoris Cryoris commented Jan 29, 2020

Summary

Implement the relative phase Toffoli gates (CCX and CCCX), which only existed as functions and not Gates. These gates are used in the definition of other multi-controlled gates and are blocking their implementation.

Details and comments

These gates follow https://arxiv.org/pdf/1508.03273.pdf. The relative-phase CCX is shown in Fig. 3, dashed box and the relative-phase CCCX is shown in Fig. 4, the whole circuit.

Things left to decide:

  • How should the gate class be called? The method used to be rccx, which conflicts with the naming scheme we introduce in Consistent naming of controlled gates #3633 where we use R to denote a rotation.
    Edit: Named rccx/RCCXGate since the method is already called rccx.
  • How should the relative phase Toffolis be drawn in the circuit drawer? Drawing them with X as base gate suggests that they are the same as the Toffolis -- which they are not.
    Edit: Resolved by not being a controlled gate, thus the name is drawn in a box (composite gate style).
  • Do these gates have a base_gate? The first idea might be to use the XGate as base gate, but this is not accurate, as the base gate implies that the controlled gate can be written as
    controlled_gate = |0><0| \times id + |1><1| \times X.
    Also this runs into problems with the default mechanisms of Qiskit to create a controlled gate.
    If they don't have a base_gate this must be accordingly dealt with in the circuit drawer.
    Edit: Resolved by not being a ControlledGate but Gate type for now.

@ajavadia
Copy link
Member

We can just call it the Margolus gate? See #1741 (item 8) and https://arxiv.org/abs/quant-ph/0312225.

For now I think drawing it with a box and its name inside makes sense, unless we can come up with a modified notation for ccx that shows a relative phase difference.

This is not a ControlledGate, and as such should not have base_gate. The matrix for it is (using big endian notation):

array([[ 1.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j],
       [ 0.+0.j,  1.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j],
       [ 0.+0.j,  0.+0.j,  1.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j],
       [ 0.+0.j,  0.+0.j,  0.+0.j,  1.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j],
       [ 0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  1.+0.j,  0.+0.j,  0.+0.j,  0.+0.j],
       [ 0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j, -1.+0.j,  0.+0.j,  0.+0.j],
       [ 0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.-1.j],
       [ 0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+1.j,  0.+0.j]])

@Cryoris
Copy link
Contributor Author

Cryoris commented Jan 29, 2020

Agreed to not having it as controlled gate, that solves the issue. 👍
The matrix representation is also the one implemented in the class at the moment (with Qiskit's ordering).

However I'm not sure about Margolus gate, since (1) we tried to get consistent "action-based" gate names, and (2) how would we call the method then?
In the paper you attached this gate is referred to as simplified Toffoli, what do you think about SimplifiedCCXGate/sccx? Or maybe keep the method name rccx, which is what its been up to now.

@ajavadia
Copy link
Member

Ok I'm fine leaving rccx (that causes least disruption for now). I would at least put Margolus / Simplified Toffoli in the documentation because that seems a more common way of referring to this gate.

There's some limit to "action-based" descriptions of gates, as gates have come to have common names in the literature and community. Like S gate and V gate, whose actions are basically rz90 and sqrt-x, but everyone just refers to them by the former names.

@Cryoris Cryoris changed the title [WIP] Implement the relative phase Toffoli gates Implement the relative phase Toffoli gates Feb 6, 2020
@Cryoris
Copy link
Contributor Author

Cryoris commented Feb 6, 2020

Ok, I left the method name as rccx/rcccx and named the classes RCCXGate/RCCCXGate. The docstrings describe them as simplified Toffoli or Margolus, or simplified 3-controlled Toffoli, respectively. If that naming is fine, the PR is ready to go from my side. 👍

Copy link
Member

@ajavadia ajavadia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good mostly, some minor comments

qiskit/extensions/standard/rcccx.py Outdated Show resolved Hide resolved
qiskit/extensions/standard/rcccx.py Outdated Show resolved Hide resolved
qiskit/extensions/standard/rcccx.py Outdated Show resolved Hide resolved
qiskit/extensions/standard/rccx.py Outdated Show resolved Hide resolved
Cryoris and others added 3 commits February 12, 2020 17:16
Co-Authored-By: Ali Javadi-Abhari <[email protected]>
Co-Authored-By: Ali Javadi-Abhari <[email protected]>
@mergify mergify bot merged commit 775b15e into Qiskit:master Feb 13, 2020
faisaldebouni pushed a commit to faisaldebouni/qiskit-terra that referenced this pull request Aug 5, 2020
* implement relative phase toffoli gates as gates

* fix CX, Cnot mixup

* update init, remove unused import

* add matrix repr, fix num_qubits

* add test on relative phase toffolis

* remove base_gate, X does not really apply

* make Gate, not ControlledGate, update docstrings

* rename to RCCX

* add qasm definitions

* update str length due to rcc(c)x definitions

* fix deprecation warning and test

* rename tgt/ctl to full names

* Correct docstring

Co-Authored-By: Ali Javadi-Abhari <[email protected]>

* Correct docstring

Co-Authored-By: Ali Javadi-Abhari <[email protected]>

Co-authored-by: Ali Javadi-Abhari <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants